﻿//using System;
//using System.Collections.Generic;
//using System.Data;
//using System.IO;
//using System.Collections.ObjectModel;
//using System.Windows.Forms;
//using PMC.Interfaces;
//using PMC.Plugins;
//using PMC.Scripts;
//using PMC.Structs;

//namespace PMC.Handlers
//{
//    public class EWebsite : IEnumerable<Website>, ISaveloadable
//    {
//        /// <summary>
//        /// Singleton
//        /// </summary>
//        private static EWebsite instance;
//        //public IHandleable noWebsiteFound;
//        //public IHandleable NoWebsiteFound
//        //{
//        //    get { return noWebsiteFound; }
//        //    set
//        //    {
//        //        if (Properties.Settings.Default.NoWebsiteFound != value.GetType().ToString())
//        //            Properties.Settings.Default.NoWebsiteFound = value.GetType().ToString(); //Only update when needed
//        //        noWebsiteFound = value;
//        //    }
//        //}
//        public static EWebsite Instance
//        {
//            get { return instance ?? (instance = new EWebsite()); }
//        }
//        public Website this[string index]
//        {
//            get { return websites.Find(c => c.Name == index); }
//        }

//        private readonly string websitesPath = Properties.Settings.Default.WebsiteFolder;
//        private readonly List<Website> websites = new List<Website>();
//        public ReadOnlyCollection<Website> Websites { get { return websites.AsReadOnly(); }}

//        /// <summary>
//        /// Singleton constructor
//        /// </summary>
//        private EWebsite()
//        {
//            Load();
//            websites.Sort();
//        }

//        public void AddWebsite(Website w, IOverrideable overrideConfirmation)
//        {
//            var duplicate = websites.Find(c => c.Name == w.Name);
//            if (duplicate != null)
//                if(overrideConfirmation.AllowOverride(w.Name) == DialogResult.OK)
//                    RemoveWebsite(duplicate);
//                else
//                    return;

//            websites.Add(w);
//        }

//        public void ChangeWebsite(Website w, string name, Uri webpage, string username, string password, Script script, IOverrideable overrideConfirmation)
//        {
//            bool isNew = false;
//            if (w == null)
//            {
//                isNew = true;
//                w = new Website();
//            }
//            w.Name = name;
//            w.Webpage = webpage;
//            w.Username = username;
//            w.Password = password;
//            w.Script = script;
//            if (isNew)
//                AddWebsite(w, overrideConfirmation);
//            changed = true;
//        }

//        public void RemoveWebsite(Website w)
//        {
//            websites.Remove(w);
//            w.Delete();
//            changed = true;
//        }

//        public void RemoveWebsite(int removeAt)
//        {
//            websites[removeAt].Delete();
//            websites.RemoveAt(removeAt);
//            changed = true;
//        }

//        /// <summary>
//        /// Gets the Website by the given Uri
//        /// </summary>
//        /// <param name="website">Current website</param>
//        /// <returns>Website matching the given uri</returns>
//        public IHandleable GetWebsiteByUri(Uri website)
//        {
//            foreach (Website w in websites)
//            {
//                string notworldwidehost = String.Join(string.Empty, w.Webpage.Host.Split(new[] {"www.", "http:", "https:", "/", @"\"}, StringSplitOptions.None));
//                if (w.Webpage.IsBaseOf(website) || website.OriginalString.IndexOf(notworldwidehost, StringComparison.OrdinalIgnoreCase) > -1)
//                    return w;
//            }
//            return Script.Default; // Needs to return an STANDARD handlers.
//        }

//        private bool changed;
//        public bool Changed
//        {
//            get { return changed; }
//        }

//        public void Load()
//        {
//            //TODO if not found
//            //NoWebsiteFound = Plugin<Script>.FindPlugin(Properties.Settings.Default.NoWebsiteFound).Instance;

//            if (!Directory.Exists(websitesPath)) //Todo maybe fix this, throw exception or some
//                return;

//            websites.Clear();
//            var files = Directory.GetFiles(websitesPath, "*.xml");
//            foreach (var file in files) //BUG if a website is not welformed it will throw an error!
//            {
//                var current = Path.GetFullPath(file);
//                var website = Website.Load(current);
//                websites.Add(website);
//            }
//        }

//        public void Save()
//        {
//            foreach (var website in Websites)
//            {
//                website.Save();
//            }
//            changed = false;
//        }

//        public void Reload()
//        {
//            Load();
//            changed = true;
//        }

//        #region Enumerable Interface Methods
//        public IEnumerator<Website> GetEnumerator()
//        {
//            return websites.GetEnumerator();
//        }

//        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
//        {
//            return GetEnumerator();
//        }
//        #endregion
//    }
//}
